import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/collectioninfomodel.dart';
import 'package:provider/provider.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '这是我的收藏页面',
      home: Scaffold(body: Container(child: LikePage())),
    );
  }
}

class LikePage extends StatefulWidget {
  const LikePage({Key key}) : super(key: key);
  @override
  _LikePageState createState() => _LikePageState();
}

class _LikePageState extends State<LikePage> {
  List list = [];
  @override
  initState() {
    super.initState();
    // 页面加载时调用 用来开始时访问数据库
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '我的收藏',
          style: TextStyle(color: Colors.black54),
        ),
        iconTheme: IconThemeData(color: Colors.black54),
        centerTitle: true,
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: Container(
          child: ListView(
        children: _list(),
      )),
    );
  }

  List<Widget> _list() {
    List<Widget> list = [];
    var newList = context
        .watch<Collectioninfomodel>()
        .collectionMap
        .forEach((key, value) {
      list.add(enshrine(
        collection: value,
      ));
    });
    return list;
  }

  // 发送请求删除并更新收藏列表

}

class enshrine extends StatelessWidget {
  Collection collection;

  enshrine({Key key, this.collection}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // String str = item['money'].toString();
    // var price = int.parse(str).toDouble().toStringAsFixed(2);
    return Padding(
      padding: const EdgeInsets.only(left: 16.0, right: 16, top: 24),
      child: Container(
        decoration: BoxDecoration(
          // border: Border.all(color: Colors.blueGrey[100]),
          color: Color(0xFFfcfcfc),
          borderRadius: BorderRadius.circular(10),
          boxShadow: [
            BoxShadow(
                color: Colors.black12,
                offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                blurRadius: 5.0, //阴影模糊程度
                spreadRadius: 0.1 //阴影扩散程度
                ),
          ],
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Slidable(
            actionPane: SlidableBehindActionPane(), //滑出选项的面板 动画
            secondaryActions: <Widget>[
              //右侧按钮列表
              // IconSlideAction(
              //   caption: 'More',
              //   color: Colors.black45,
              //   icon: Icons.more_horiz,
              //   // onTap: () => _showSnackBar('More'),
              // ),
              IconSlideAction(
                caption: 'Delete',
                color: Colors.red,
                icon: Icons.delete,
                closeOnTap: true,
                onTap: () {
                  // _showSnackBar('Delete');
                  var newList = context
                      .read<Collectioninfomodel>()
                      .changecollectionstzte(collection.commodityId);
                },
              ),
            ],

            actionExtentRatio: 0.25,
            child: Container(
              decoration: BoxDecoration(
                // border: Border.all(color: Colors.blueGrey[100]),
                color: Color(0xFFfcfcfc),
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black12,
                      offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                      blurRadius: 5.0, //阴影模糊程度
                      spreadRadius: 0.1 //阴影扩散程度
                      ),
                ],
              ),
              height: 140,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(12, 12, 12, 12),
                child: Row(
                  children: [
                    Expanded(
                      flex: 2,
                      child: Container(
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Image.network(collection.imgurl1,
                              fit: BoxFit.contain),
                        ),
                        width: 112,
                        height: 112,
                        // color: Colors.green[100],
                      ),
                    ),
                    Expanded(
                      flex: 3,
                      child: Container(
                        // width: 174,
                        // color: Colors.amber[100],
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Text(collection.name,
                                      style: TextStyle(
                                          fontSize: 20,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.blueGrey[800])),
                                ],
                              ),
                              Container(
                                // width: 140,
                                child: Text(
                                  collection.intro,
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Colors.blueGrey[300],
                                    fontSize: 13,
                                  ),
                                ),
                              ),
                              Text(
                                '￥${collection.money}',
                                style: TextStyle(
                                  color: Colors.blue[800],
                                  fontSize: 18,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
